package avicit.bdp.dgs.qa.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dgs.qa.dao.RuleVariableDAO;
import avicit.bdp.dgs.qa.dto.RuleDTO;
import avicit.bdp.dgs.qa.dto.RuleVariableDTO;
import avicit.platform6.commons.utils.ComUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-07-04
 * @类说明：RuleVariableService
 * @修改记录：
 * @注意事项：
 * @主要功能：规则参数Service
 */
@Service
public class RuleVariableService extends BaseService<RuleVariableDAO, RuleVariableDTO> {
    private static final Logger logger = LoggerFactory.getLogger(RuleVariableService.class);

    /**
     * 新增规则参数
     * @param ruleDTO
     */
    public void addVariable(RuleDTO ruleDTO) {
        if (ruleDTO == null) {
            return;
        }
        if (StringUtils.isEmpty(ruleDTO.getId())) {
            return;
        }
        if (StringUtils.isEmpty(ruleDTO.getVariables())) {
            return;
        }

        RuleVariableDTO ruleVariable = new RuleVariableDTO();
        ruleVariable.setId(ComUtil.getId());
        ruleVariable.setRuleId(ruleDTO.getId());
        ruleVariable.setArguementValue(ruleDTO.getVariables());

        this.insert(ruleVariable);
        BdpLogUtil.log4Insert(ruleVariable);
    }

    /**
     * 删除规则参数
     * @param ruleId
     */
    public void deleteVariable(String ruleId) {
        if (StringUtils.isEmpty(ruleId)) {
            return;
        }

        this.mapper.deleteVariableByCond(ruleId);
    }

    /**
     * 修改规则参数
     * @param ruleDTO
     */
    public void updateVariable(RuleDTO ruleDTO) {
        if (ruleDTO == null) {
            return;
        }
        if (StringUtils.isEmpty(ruleDTO.getId())) {
            return;
        }
        if (StringUtils.isEmpty(ruleDTO.getVariables())) {
            return;
        }

        RuleVariableDTO ruleVariable = this.mapper.getVariableByCond(ruleDTO.getId());
        if (ruleVariable == null) {
            return;
        }

        ruleVariable.setArguementValue(ruleDTO.getVariables());

        this.updateByPrimaryKeySelective(ruleVariable);
    }

    /**
     * 根据规则ID查询参数
     * @param ruleId
     * @return
     */
    public RuleVariableDTO getVariableByRuleId(String ruleId) {
        if (StringUtils.isEmpty(ruleId)) {
            return null;
        }

        return this.mapper.getVariableByCond(ruleId);
    }
}
